package edu.jmu.bean.privilege;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

import edu.jmu.bean.user.User;

@Entity
public class Role implements Serializable{
	
	/**d
	 * 
	 */
	private static final long serialVersionUID = 1736541437824111852L;
	
	/** 主键	**/
	private Integer roleId;
	/**	角色名称	**/
	private String name;
	/**	角色描述	**/
	private String description;
	/**	角色权限 	**/
	private Set<Privilege> privileges = new HashSet<Privilege>();
	/**	用户	**/
	private Set<User> users = new HashSet<User>();
	
	@Id	@GeneratedValue(strategy=GenerationType.AUTO)
	public Integer getRoleId() {
		return roleId;
	}
	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}
	@Column(length=20,nullable=false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Column(length=100)
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	@ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.EAGER)
	@JoinTable(name="role_privilege",inverseJoinColumns=@JoinColumn(name="id"),joinColumns=@JoinColumn(name="role_id"))
	public Set<Privilege> getPrivileges() {
		return privileges;
	}
	public void setPrivileges(Set<Privilege> privileges) {
		this.privileges = privileges;
	}
	
	public void addPrivileges(Privilege privilege){
		this.privileges.add(privilege);
	}
	
	@ManyToMany(mappedBy="roles",cascade=CascadeType.REFRESH)
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((roleId == null) ? 0 : roleId.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Role other = (Role) obj;
		if (roleId == null) {
			if (other.roleId != null)
				return false;
		} else if (!roleId.equals(other.roleId))
			return false;
		return true;
	}
}
